<?php
	class admin extends BaseController{
		public $security_level = SECURITY_LEVEL_ADMIN;
		public function getData($in_params){
			$apply = new m_apply();
			$applyright = new m_apply_rights();
			$user= new m_user();
			$room = new m_room;
			$data3 = $user->select();
			$data = $apply->select();
			$sql_join_rooms=" LEFT JOIN `rooms` ON `apply_rights`.`room_id` = `rooms`.`room_id`";
			$sql_join_persons=" LEFT JOIN `users` ON `apply_rights`.`user_id` = `users`.`id`";
			$sql="SELECT *,`apply_rights`.`id` AS `apply_id` FROM `apply_rights`" . $sql_join_persons . $sql_join_rooms;
			$data5 = $applyright->query($sql);
			$this->template_data["all"]=$data5;
			$data2 = $applyright->select();
			$data4 = $room->select();
			$sub=$_REQUEST;
			
			
			$_SESSION['warnings']=count($data)+count($data5);

			$this->template_data["applications"] = $data;
			$this->template_data["sub"]=$sub;
			$this->template_data["rooms"]=$data1;
			$this->template_data["rights"]=$data2;
			$this->template_data["users"]=$data3;
			$this->template_data["room"]=$data4;
			
			$this->template_data["applyid"]=$_GET['applyid']; // pokus
			

			return;
		}
		
		public function action_addUser($in_params){
			$email = $in_params['email'];
			$apply = new m_apply();
			$data = $apply->select('',array("email"=>$_REQUEST["email"]));
			$user= new m_user();
			$user->email=$data[0]->email;
			$user->password=$data[0]->password;
			$user->security_level=10;
			$user->name=$data[0]->name;
			$user->surname=$data[0]->surname;
			$user->insert();
			$apply->delete("","id");
			general::redirect('home?list=warnings&sub=registration');
			
		}
		
		public function action_deleteUser($in_params){
			$email = $in_params['email'];
			$apply = new m_apply();
			$data = $apply->select('',array("email"=>$_REQUEST["email"]));
			$apply->delete("","id");
			general::redirect('home?list=warnings&sub=registration');
		
		}
		
		public function action_addRight($in_params){
			$_SESSION["debug"] = $in_params;
			$id = $in_params['id'];
			$apply_rights = new m_apply_rights();
			$rights = new m_rights();
			$apply_rights->select('',array("id"=>$_REQUEST["id"]));
			$rights->select("", array("user_id"=>$apply_rights->user_id,"room_id"=>$apply_rights->room_id));
			$rights->user_id=$apply_rights->user_id;
			$rights->room_id=$apply_rights->room_id;
			$rights->add    = !empty($in_params["add"]);
			$rights->watch  = !empty($in_params["watch"]);
			$rights->edit   = !empty($in_params["edit"]);
			$rights->delete = !empty($in_params["delete"]);
			$_SESSION["debugugubu"] = $rights;
			if((!$rights->add)&&(!$rights->watch)&&(!$rights->edit)&&(!$rights->delete)){
				$rights->delete("", "right_id");
				$apply_rights->delete("","id");
				general::redirect('home?list=warnings&sub=rights');
			}else{
				$rights->insertUpdate("", "right_id");
				$apply_rights->delete("","id");
				general::redirect('home?list=warnings&sub=rights');
			}			
		}
		
		public function action_deleteRight($in_params){
			$id = $in_params['id'];
			$apply_rights = new m_apply_rights();
			$data = $apply_rights->select('',array("id"=>$_REQUEST["id"]));
		
			$apply_rights->delete("","id");
			general::redirect('home?list=warnings&sub=rights');
			
		}
		
		public function action_addRoom($in_params){
			$id = $in_params['id'];
			$apply_rooms = new m_apply_rooms();
			$data = $apply_rooms->select('',array("id"=>$_REQUEST["id"]));
			$user_id = $data[0]->user_id;
			$room_id = $data[0]->room_id;		
			$room= new m_rights();
			$room->user_id=$user_id;
			$room->room_id=$room_id;		
			$room->insert();		
			$apply_rooms->delete("","id");
			general::redirect('home?list=warnings&sub=rooms');
			
		}
		
		public function action_deleteRoom($in_params){
			$id = $in_params['id'];
			$apply_rooms = new m_apply_rooms();
			$data = $apply_rooms->select('',array("id"=>$_REQUEST["id"]));
			$apply_rooms->delete("","id");
			general::redirect('home?list=warnings&sub=rooms');
			
		}
	}
?>